﻿/*************************************************************************************
**
** Library: QEXT
**
** Copyright (C) 2021 ChengXueWen. Contact: 1398831004@qq.com
** Copyright (C) 2016 feiyangqingyun. Contact: QQ:517216493
**
** License: MIT License
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
**
*************************************************************************************/

#ifndef _QEXTPROGRESSKNOB_H
#define _QEXTPROGRESSKNOB_H

#include <qextWidgetGlobal.h>

#include <QWidget>
#include <QVariant>

class QPropertyAnimation;
class QEXTProgressKnobPrivate;
class QEXT_WIDGETS_API QEXTProgressKnob : public QWidget
{
    Q_OBJECT

    Q_PROPERTY(double minValue READ minValue WRITE setMinValue)
    Q_PROPERTY(double maxValue READ maxValue WRITE setMaxValue)
    Q_PROPERTY(double value READ value WRITE setValue)
    Q_PROPERTY(int precision READ precision WRITE setPrecision)

    Q_PROPERTY(int startAngle READ startAngle WRITE setStartAngle)
    Q_PROPERTY(int endAngle READ endAngle WRITE setEndAngle)

    Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
    Q_PROPERTY(QColor progressColor READ progressColor WRITE setProgressColor)
    Q_PROPERTY(QColor progressBackgroundColor READ progressBackgroundColor WRITE setProgressBackgroundColor)
    Q_PROPERTY(QColor circleStartColor READ circleStartColor WRITE setCircleStartColor)
    Q_PROPERTY(QColor circleEndColor READ circleEndColor WRITE setCircleEndColor)
    Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)

    Q_PROPERTY(bool pointerVisiable READ pointerVisiable WRITE setPointerVisiable)
    Q_PROPERTY(bool valueVisiable READ valueVisiable WRITE setValueVisiable)
    Q_PROPERTY(PointerStyle pointerStyle READ pointerStyle WRITE setPointerStyle)

public:
	enum PointerStyle {
        PointerStyle_Circle = 0,
        PointerStyle_Indicator,
        PointerStyle_IndicatorR,
        PointerStyle_Triangle
	};
    Q_ENUMS(PointerStyle)

    explicit QEXTProgressKnob(QWidget *parent = QEXT_DECL_NULLPTR);
    ~QEXTProgressKnob();

    double minValue() const;
    double maxValue() const;
    double value() const;
    int precision() const;

    int startAngle() const;
    int endAngle() const;

    QColor backgroundColor() const;
    QColor progressColor() const;
    QColor progressBackgroundColor() const;
    QColor circleStartColor() const;
    QColor circleEndColor() const;
    QColor textColor() const;

    bool pointerVisiable() const;
    bool valueVisiable() const;
    PointerStyle pointerStyle() const;

    QSize sizeHint() const;
    QSize minimumSizeHint() const;

public Q_SLOTS:
    void setRange(double minValue, double maxValue);
    void setRange(int minValue, int maxValue);

    void setMinValue(double minValue);
    void setMaxValue(double maxValue);

    void setValue(double value);
    void setValue(int value);

    void setPrecision(int precision);

    void setStartAngle(int angle);
    void setEndAngle(int angle);

    void setBackgroundColor(const QColor &color);

    void setProgressColor(const QColor &color);
    void setProgressBackgroundColor(const QColor &color);

    void setCircleStartColor(const QColor &color);
    void setCircleEndColor(const QColor &color);

    void setTextColor(const QColor &color);

    void setPointerVisiable(bool visiable);
    void setValueVisiable(bool visiable);
    void setPointerStyle(PointerStyle style);

Q_SIGNALS:
    void valueChanged(int value);

protected:
	void enterEvent(QEvent *);
	void leaveEvent(QEvent *);
	void mousePressEvent(QMouseEvent *);
	void mouseReleaseEvent(QMouseEvent *);
	void mouseMoveEvent(QMouseEvent *);
	void paintEvent(QPaintEvent *);

    void drawBackground(QPainter *painter);
	void drawColorPie(QPainter *painter);
	void drawCoverCircle(QPainter *painter);
	void drawCircle(QPainter *painter);
	void drawPointerCircle(QPainter *painter);
	void drawPointerIndicator(QPainter *painter);
	void drawPointerIndicatorR(QPainter *painter);
	void drawPointerTriangle(QPainter *painter);
    void drawValue(QPainter *painter);

    QScopedPointer<QEXTProgressKnobPrivate> dd_ptr;

private slots:
    void setEasingCurve();
    void updateRadius(QVariant radius);

private:
    void setPressedValue(QPointF pressedPoint);

    QEXT_DECL_DISABLE_COPY_MOVE(QEXTProgressKnob)
    QEXT_DECL_PRIVATE_D(dd_ptr, QEXTProgressKnob)
};

#endif // _QEXTPROGRESSKNOB_H
